from selenium.webdriver.support.wait import WebDriverWait
from appium.webdriver.common.appiumby import By

from appPage.enter_homepage import EnterHomePage
from appPage.login import Login
from appPage.search import Search
from config import PHONE, PWD, SEARCH_TEXT
from utils.driver_utils import DriverUtils


class BuyNow:
    def buy_now(self, driver):
        WebDriverWait(driver, 10).until(lambda x: x.find_element(by=By.ID, value="com.tpshop.malls:id/promptly_buy_tv")).click()
        WebDriverWait(driver, 10).until(lambda x: x.find_element(by=By.XPATH, value="//*[@text='套餐二']")).click()
        WebDriverWait(driver, 10).until(lambda x: x.find_element(by=By.XPATH, value="//*[@text='极光色']")).click()
        WebDriverWait(driver, 10).until(lambda x: x.find_element(by=By.XPATH, value="//*[@text='全网通4G+64G']")).click()
        # 滑动界面
        driver.swipe(start_x=740, start_y=1660, end_x=720, end_y=1200)
        WebDriverWait(driver, 10).until(lambda x: x.find_element(by=By.ID, value="com.tpshop.malls:id/cart_plus_btn")).click()
        WebDriverWait(driver, 10).until(lambda x: x.find_element(by=By.ID, value="com.tpshop.malls:id/confirm_tv")).click()

if __name__ == '__main__':
    # 驱动
    driver_utils = DriverUtils()
    driver = driver_utils.get_driver()
    # 进入首页
    enter = EnterHomePage()
    # 登录
    login = Login()
    # 搜索
    search = Search()
    # 立即购买
    buy_now = BuyNow()
    enter.enter_homepage(driver)
    login.login(driver, PHONE, PWD)
    search.search(driver, SEARCH_TEXT)
    buy_now.buy_now(driver)
    driver_utils.quit_driver()